package edu.washington.cs.workflow.spec4j.example;

import org.uispec4j.Button;
import org.uispec4j.Panel;
import org.uispec4j.Trigger;
import org.uispec4j.UIComponent;
import org.uispec4j.UISpec4J;
import org.uispec4j.UISpecTestCase;
import org.uispec4j.finder.ComponentMatcher;
import org.uispec4j.interception.MainClassAdapter;

//import edu.washington.cs.workflow.analysis.guistructure.MonkeyTestingMain;
import edu.washington.cs.workflow.analysis.guistructure.Spec4JUtils;

import samples.calculator.CalculatorPanel;

public class TestCalculator extends UISpecTestCase{

	static {
        UISpec4J.init();
        UISpec4J.setWindowInterceptionTimeLimit(2000);
    } 
	
	protected void setUp() throws Exception {
	    setAdapter(new MainClassAdapter(CalculatorPanel.class, new String[0]));
	    super.setUp();
	}
	
	public void testSimpleAddition() {
		  Panel calculatorPanel = getMainWindow();
		  
		  calculatorPanel.getButton("2").click();
		  calculatorPanel.getButton("+").click();
		  calculatorPanel.getButton("6").click();
		  calculatorPanel.getButton("=").click();
		  System.out.println(calculatorPanel.getTextBox().getText());
		  assertTrue(calculatorPanel.getTextBox().textEquals("8"));
	}
	
	public void testObjectComp() {
		  Panel calculatorPanel = getMainWindow();
		  
		  Button b1 = calculatorPanel.getButton("2");
		  Button b2 = calculatorPanel.getButton("2");
		  
		  Button b3 = calculatorPanel.getButton("3");
		  
		  System.out.println(b1.equals(b2));
		  System.out.println(b1.getAwtComponent().equals(b2.getAwtComponent()));
		  
		  System.out.println(b1.equals(b3));
		  System.out.println(b1.getAwtComponent().equals(b3.getAwtComponent()));
	}
	
	public void testObjectCompAcrossRuns1() throws Exception {
		  Panel calculatorPanel = getMainWindow();
		  
		  calculatorPanel.getButton("2").click();
		  calculatorPanel.getButton("+").click();
		  calculatorPanel.getButton("6").click();
		  calculatorPanel.getButton("=").click();
		  System.out.println(calculatorPanel.getTextBox().getText());
	}
	
	public void testObjectCompAcrossRuns2() {
		  Panel calculatorPanel = getMainWindow();
		
//		  calculatorPanel.getButton("2").click();
		  calculatorPanel.getButton("+").click();
		  calculatorPanel.getButton("6").click();
		  calculatorPanel.getButton("=").click();
		  System.out.println(calculatorPanel.getTextBox().getText());
	}
	
	public void testClickEveryButton() {
		try {
			clickEveryButton();
			fail("Expecting: number format exception.");
		} catch (NumberFormatException e) {
			//ok
		}
	}
	
	public void clickEveryButton() {
		Panel calculatorPanel = getMainWindow();
		  
		  UIComponent[] uis = calculatorPanel.getUIComponents(ComponentMatcher.ALL);
		  
		  System.out.println(uis.length);
		  for(UIComponent ui : uis) {
			  if(ui != null) {
			      if(ui instanceof Button) {
			    	  System.out.println("click: " + ui.getLabel() + "  of " + ui.getClass());
			    	  ((Button) ui).click();
			      }
			  }
		  }
	}
	
	public void testPopDialog() {
		Panel calculatorPanel = getMainWindow();
		calculatorPanel.getButton("2").click();
		
		
		//compare
		System.out.println(calculatorPanel.getButton("2").getContainer().getContainer().getContainer().getContainer().getContainer().getContainer());
		System.out.println(System.identityHashCode(calculatorPanel.getButton("2")));
	
		Button b = calculatorPanel.getButton("2");
		Trigger t = b.triggerClick();
		System.out.println(t);
		
		System.out.println(Spec4JUtils.getTriggeredDialog(b, t));
		
//		try {
//			Window w = WindowInterceptor.run(t);
//		} catch (Throwable e) {
//			System.err.println(e.getMessage());
//			e.printStackTrace();
//		}
		
		try {
			
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}
	
//	public void testMonkeyTester() {
//		MonkeyTestingMain.main(new String[]{"samples.calculator.CalculatorPanel"});
//	}
	
}
